package com.lenovo.manufacture;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.beardedhen.androidbootstrap.BootstrapProgressBar;
import com.beardedhen.androidbootstrap.BootstrapText;
import com.beardedhen.androidbootstrap.TypefaceProvider;
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapHeading;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.lenovo.manufacture.annotation.Layout;
import com.lenovo.manufacture.net.ApiService;
import com.lenovo.manufacture.net.RetrofitFactory;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BaseActivity extends AppCompatActivity {
    private static final String TAG = "BaseActivity";

    private TextView title;
    public ApiService apiService = RetrofitFactory.getApiService();
    public Map<String, Object> json = new HashMap<>();
    protected ParamBuilder param = new ParamBuilder();
    public DecimalFormat decimalFormat = new DecimalFormat();
    public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    public ProgressDialog progressDialog;
    Toast toast;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TypefaceProvider.registerDefaultIconSets();
        initActionBar();
        initDialog();
        initLayout();
        init();
    }

    private void initLayout() {
        try {
            Layout init = getClass().getDeclaredMethod("init").getAnnotation(Layout.class);
            if (init == null) return;
            setContentView(init.value());
            ButterKnife.bind(this);
            title.setText(init.title());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    private void initDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("加载中");
    }

    protected void init() {
    }

    private void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(R.layout.title);
        ImageView back = actionBar.getCustomView().findViewById(R.id.iv_back);
        back.setOnClickListener(view -> finish());
        title = actionBar.getCustomView().findViewById(R.id.tv_title);
    }

    public <T> void async(Call<T> call, Callable<T> callable) {

        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                Log.i(TAG, "async: " + response.toString());
                Log.i(TAG, "param: " + json.toString());
                if (response.isSuccessful()) {
                    Log.i(TAG, "onResponse: " + response.body().toString());
                    callable.Call(response.body());
                } else {
                    showMsg("失败");
                }
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                t.printStackTrace();
                showMsg("获取数据失败");
            }
        });
    }

    public void showMsg(String msg) {
        if (toast != null) {
            toast.cancel();
        }
        toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        BootstrapLabel bootstrapLabel = new BootstrapLabel(this);
        bootstrapLabel.setText(msg);
        bootstrapLabel.setBootstrapBrand(DefaultBootstrapBrand.DANGER);
        bootstrapLabel.setBootstrapHeading(DefaultBootstrapHeading.H1);
        bootstrapLabel.setRounded(true);
        toast.setView(bootstrapLabel);
        toast.show();
    }

    public <T> void sort(List<T> list, String key, BootstrapLabel textView, String text) {
        boolean desc = textView.getTag() != null && ((boolean) textView.getTag());
        Collections.sort(list, (o1, o2) -> {
            Map<String, String> o11 = (Map<String, String>) o1;
            Map<String, String> o22 = (Map<String, String>) o2;
            if (o11.get(key).matches("\\d+")) {
                return desc ? Integer.compare(Integer.parseInt(o11.get(key)), Integer.parseInt(o22.get(key))) : Integer.compare(Integer.parseInt(o22.get(key)), Integer.parseInt(o11.get(key)));
            }
            return desc ? o11.get(key).compareTo(o22.get(key)) : o22.get(key).compareTo(o11.get(key));
        });
        String arrow = desc ? FontAwesome.FA_CARET_UP : FontAwesome.FA_CARET_DOWN;
        textView.setTag(!desc);
        textView.setBootstrapText(new BootstrapText.Builder(this).addText(text + " ").addFontAwesomeIcon(arrow).build());
        String s = desc ? "升序" : "降序";
        showMsg("按" + text + s);
    }

    protected class ParamBuilder {
        public ParamBuilder add(String k, Object value) {
            json.put(k, value);
            return this;
        }

        public Map<String, Object> build() {
            return json;
        }
    }

    public interface Callable<T> {
        void Call(T data);
    }
}
